<?php
namespace Admin\Controller;
use Think\Controller;

class CategoryController extends CommonController
{
    private $Category;

    function __construct()
    {
        parent::__construct();
        $this->Category=D('Category');
    }

    public function index()
    {

        //$categories = $this->Category->where('parent_id=0')->order('sort_order')->select();
        //foreach ($categories as &$v){

         //   $v['children']=$this->Category->where("parent_id=$v[id]")->order('sort_order')->select();
            //dump($v);
        //}
        //$categories = $this->Category->where('parent_id=0')->relation(true)->order('sort_order')->select();
        //p($categories);
        $categories = $this->Category->order('sort_order')->select();
        //p($categories);
        $categories = $this->Category->reSort($categories);
        //p($categories);
        //exit();
        $this->assign('categories',$categories);
        $this->display();
    }

    public function create(){

        $id = empty((I('get.id')))?0:I('get.id');
        $cate = $this->Category->find($id);
        //p($cate);
        //die;

        $categories = $this->Category->order('sort_order')->select();
        $categories = $this->Category->reSort($categories);

        $this->assign('categories',$categories);
        $this->assign('cate',$cate);
        $this->display();
    }

    public function store(){
//        dump($_POST);
//        die;

        if ($this->Category->create()){
            // 验证通过 可以进行其他数据操作
            if($this->Category->add()){
                $this->success('新增栏目成功！','index');
                return;
            }
        }
        // 如果创建失败 表示验证没有通过 输出错误提示信息
        $this->error($this->Category->getError());
    }


    public function sort(){
        //p($_POST);
        $sort_id = I('post.sort_id');
        $sort_order = I('post.sort_order');
        foreach ($sort_id as $k=>$v){
            $this->Category->where("id=$v")->setField('sort_order',"$sort_order[$k]");
        }
        $this->redirect('index');
    }

    public function del(){
        //得到要删除的栏目id
        $del_id = I('get.id');
        //1、判断他是不是叶子节点，也就是没有子栏目；2、该栏目有文章也不能删除
        $Article=M('Article');
        $article = $Article->where("category_id='$del_id'")->find();
        //dump($article);
        //die;
        if($this->Category->where("parent_id='$del_id'")->find()){
            $this->error('有子栏目不能删除！');
        }
        if($Article->where("category_id='$del_id'")->find()){
            $this->error('栏目有文章不能删除！');
        }

        $this->Category->where("id='$del_id'")->delete();
        $this->redirect('index');
    }

    public function edit(){
        $edit_id = I('get.id');
        $edit_category = $this->Category->find($edit_id);
        $categories = $this->Category->order('sort_order')->select();
        $categories = $this->Category->reSort($categories);
        //p($categories);
        //die;
        $this->assign('edit_category',$edit_category);
        $this->assign('categories',$categories);
        $this->display();
    }

    public function do_edit(){
        //dump($_POST);
        //die;
        if($this->Category->create()){
            if($this->Category->save()){
                $this->success('修改成功！','index');
            }else{
                $this->error('修改失败，请重试！');
            }
        }
    }

}